package com.abu.edums.controller;


import com.abu.edums.service.CourseService;
import com.abu.exceptionhandler.EduException;
import com.abu.pojo.Course;
import com.abu.pojo.teacherQuery;
import com.abu.pojo.Teacher;
import com.abu.edums.service.TeacherService;
import com.abu.utils.Resp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2020-06-15
 */
@RestController
@RequestMapping("eduservice/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    /**
     * 查询所有老师
     *
     * @return
     */
    @GetMapping("findAll")
    public Resp queryAll() {

        List<Teacher> teacherList = teacherService.list();

        if (CollectionUtils.isEmpty(teacherList)) {
            return Resp.error();
        }
        return Resp.ok().data("items", teacherList);
    }


    @PostMapping("pageTeacherCondition/{page}/{size}")
    public Resp queryPage(@PathVariable("page") Long page, @PathVariable("size") Long size, @RequestBody(required = false) teacherQuery searchVO) {

//        构造分页条件
        Page<Teacher> teacherPage = null;
        Page<Teacher> pageTeacher = new Page<>(page, size);

//      判断查询条件是否为空
        if (searchVO == null) {
            teacherPage = this.teacherService.page(pageTeacher);
            List<Teacher> teacherlist = teacherPage.getRecords();
            long total = teacherPage.getTotal();
            return Resp.ok().data("total", total).data("rows", teacherlist);
        }
//        构造查询条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(searchVO.getName())) {
            wrapper.like("name", searchVO.getName());
        }
        if (StringUtils.isNotBlank(searchVO.getLevel())) {
            wrapper.eq("level", searchVO.getLevel());
        }
        if (searchVO.getBegin() != null) {
            wrapper.gt("gmt_create", searchVO.getBegin());
        }
        if (searchVO.getEnd() != null) {
            wrapper.le("gmt_modified", searchVO.getEnd());
        }
//        执行查询
        teacherPage = this.teacherService.page(pageTeacher, wrapper);
//        获取结果集
        List<Teacher> teacherList = teacherPage.getRecords();
        long total = teacherPage.getTotal();
        return Resp.ok().data("total", total).data("teacherlist", teacherList);
    }

    @PostMapping("addTeacher")
    public Resp saveTeacher(@RequestBody Teacher teacher) {
//        1.查询教师是否已经存在
        Teacher teacherResult = this.teacherService.getOne(new QueryWrapper<Teacher>().eq("name", teacher.getName()));
//        2.如果存在则抛出异常
        if (teacherResult != null) {
            throw new EduException(20001, "该用户名已经存在了");
        }
//        3.否则添加成功
        boolean flag = this.teacherService.save(teacher);
        if (flag) {
            return Resp.ok();
        }
        return Resp.error();
    }

    @DeleteMapping("/{id}")
    public Resp deleteOne(@PathVariable("id") String id) {
        boolean flag = this.teacherService.removeById(id);
        if (flag) {
            return Resp.ok();
        }
        return Resp.error();
    }

    @PostMapping("updateTeacher")
    public Resp update(@RequestBody Teacher teacher) {
        boolean flag = this.teacherService.updateById(teacher);
        if (flag) {
            return Resp.ok();
        }
        return Resp.error();
    }

    @GetMapping("getTeacher/{id}")
    public Resp getTeacher(@PathVariable("id") String id) {
        Teacher teacher = this.teacherService.getById(id);
        if (teacher != null) {
            return Resp.ok().data("teacher", teacher);
        }
        return Resp.error();
    }

    @GetMapping("index/teachers")
    public List<Teacher> queryIndexTeachers() {
        List<Teacher> teacherList = this.teacherService.queryLimitByLevel();
        if (!CollectionUtils.isEmpty(teacherList)) {
            return teacherList;
        }
        return null;
    }


}

